<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <el-form
        label-width="130px"
        :model="subjectForm"
        style="padding-right: 30px;"
        :rules="planSubjectRules"
        ref="subjectForm"
      >
        <el-row>
          <el-col :span="22" style="margin-top: 27px">
            <el-form-item class="form-item" label="计划名称 :" prop="title">
              <el-input v-model="subjectForm.title" placeholder="请输入计划名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="计划说明 :" prop="description">
              <el-input
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4}"
                v-model="subjectForm.description"
                placeholder="请输入计划说明"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item class="form-item" label="编制时间 :" prop="time">
              {{subjectForm.time}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="card-body">
        <avue-crud :data="subjectOpinionListData"
                   :option="tableoption"
                   :page="page"
                   :table-loading="tableLoading"
                   @on-load="getResultList"
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   class="avuecrud">

        </avue-crud>
      </div>
      <div slot="footer" class="btn-footer">
        <el-button type="primary" @click="confirmYes()">确 定</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </div>

      <el-dialog title="选择牵头单位" :visible.sync="dialogVisibleEditDept" width="1000px">
        <div style="margin-left: 47px;margin-top: -18px;">
          <p style="font-weight: 600">当前牵头单位 :{{deptNameEditDept}}</p>
        </div>
        <el-form
          label-width="130px"
          :model="projectFormEditDept"
          style="padding-right: 30px;"
          :rules="infosrules"
          ref="projectFormEditDept"
        >
          <el-row>
            <el-col :span="16">
              <el-form-item label="牵头单位 :" prop="orgCode" class="form-item">
                <span style="font-size: 16px">{{projectFormEditDept.supportDeptName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="clickjointEditDept">选择牵头单位
              </el-button>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="comitYesEditDept()">确 定</el-button>
          <el-button @click="cancelEditDept()">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog title="选择配合单位" :visible.sync="dialogVisibleEditDeptassort" width="1000px">
        <div style="margin-left: 47px;margin-top: -18px;">
          <p style="font-weight: 600">当前配合单位 :{{deptNameEditDeptassort}}</p>
        </div>
        <el-form
          label-width="130px"
          :model="projectFormEditDeptassort"
          style="padding-right: 30px;"
          :rules="infosrulesassort"
          ref="projectFormEditDeptassort"
        >
          <el-row>
            <el-col :span="16">
              <el-form-item label="配合单位 :" prop="orgCode" class="form-item">
                <span style="font-size: 16px">{{projectFormEditDeptassort.deptNameEditDeptassort}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="clickjointassortEditDept">选择配合单位
              </el-button>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="comitYesEditDeptassort()">确 定</el-button>
          <el-button @click="dialogVisibleEditDeptassort = false">取 消</el-button>
        </div>
      </el-dialog>
      <dept-select :visible="deptVisible" @getSelectInfo="getSelectInfo" @cancel="cancelDept"></dept-select>
      <dept-check :visible="supportDeptVisible" @getCheckInfo="getSupportDeptCheckInfo"
                  @cancel="cancelSupportDept"></dept-check>
    </div>
  </basic-container>
</template>

<script>
  import {getPlanSubjectList, addSubjectPlan, subjectEditDept, getSubjectInfo} from "@/api/subject";
  import {planSubjectRules} from "@/const/infoRules";

  export default {
    name: "preparationPlan",
    created() {
      var self = this;
      window.subjectDetail = self.subjectDetail;
      window.editMainDept = self.editMainDept;
      window.editMainDeptassort = self.editMainDeptassort;
      window.editAssortDept = self.editAssortDept;
      this.subjectForm.title = '《'+this.$route.query.achievementTitle+'》前期研究课题计划';
        this.putTime();
      this.putSubjectOpinionListData();
    },
    data() {
      return {
        headerData: {
          title: '计划编制'
        },
        subjectOpinionListData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoading: false,
        tableoption: {
          page: true,
          menuWidth: 220,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          menu: false,
          column: [
            {
              label: "课题名称",
              width: 250,
              prop: "title",
              cell: true,
              formatter: function (row, value, label, column) {
                return '<a style="cursor: pointer;" onclick="subjectDetail(\'' + row.subjectId + '\')">' + value + '</a>';
              },
            },
            {
              label: "课题概述",
              width: 330,
              prop: "description",
              overHidden:true,
              cell: true
            },
            {
              label: "拟牵头单位",
              prop: "deptName",
              cell: true,
              formatter: function (row, value, label, column) {
                if (row.deptName == null) {
                  value = '请选择'
                }
                return '<a style="cursor: pointer;text-decoration:underline;" onclick="editMainDept(' + row.subjectId + ')">' + value + '</a>';
              }
            },
            {
              label: "拟配合单位",
              prop: "supportDeptName",
              cell: true,
              formatter: function (row, value, label, column) {
                if (row.supportDeptName == null) {
                  value = '请选择'
                }
                return '<a style="cursor: pointer;text-decoration:underline;" onclick="editMainDeptassort(' + row.subjectId + ')">' + value + '</a>';
              }
            },
          ]
        },
        subjectForm: {
          title: '',
          description: '',
          achievementId: 0,
          subjectIds: '',
          time: '',
        },
        dialogVisibleEditDept: false,
        projectFormEditDept: {    //牵头部门表单
          supportDeptName: '',
          deptId: 0,
          supportDeptNameassort: '',
          subjectId: 0,
          excludeNull:true,
        },
        projectFormEditDeptassort: {   //配合部门表单
          supportDeptName: '',
          supportDeptNameassort: '',
          supportDeptIds: '',
          subjectId: 0,
          excludeNull:true,
        },
        deptNameEditDept: '',
        deptVisible: false,
        supportDeptVisible: false,
        dialogVisibleEditDeptassort: false,
        deptNameEditDeptassort: '',
        planSubjectRules: planSubjectRules,
      }
    },
    methods: {
      // 课题详情跳转
      subjectDetail(subjectId) {
        let src = `/subjectDetail/detail/${subjectId}`;
        this.$router.push({
          path: src
        })
      },
      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        this.getResultListPage(page);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        this.getResultListPage(page);
      },
      getResultListPage(page) {
        this.page.currentPage = page.currentPage;
        this.page.pageSize = page.pageSize;
        var selectedSubjectIdsARR = this.$route.query.selectedSubjectIds.split(',');
        var subjectIds = selectedSubjectIdsARR.slice(page.pageSize * (page.currentPage - 1), page.pageSize * page.currentPage);
        getPlanSubjectList(Object.assign({
          subjectIds: subjectIds.join(','),
        })).then(response => {
          this.subjectOpinionListData = response.data.data.records;
        })
      },
      //得到表数据的方法之所以不写在getResultList里，是因为分页会触发getResultList
      putSubjectOpinionListData(){
        getPlanSubjectList(Object.assign({
          current: 1,
          size: 10,
          subjectIds: this.$route.query.selectedSubjectIds,
        })).then(response => {
          this.subjectOpinionListData = response.data.data.records;
          this.page.total  = response.data.data.total;
        })
      },
      getResultList() {
      },
      //配置牵头部门
      async editMainDept(subjectId) {
        this.projectFormEditDept.supportDeptName = '';
        this.projectFormEditDept.deptId = 0;  //改为0防止没发做验证
        await getSubjectInfo(subjectId).then(response => {
          this.deptNameEditDept = response.data.data.deptName;
        });
        this.projectFormEditDept.subjectId = subjectId;
        this.dialogVisibleEditDept = true;
      },
      //配置配合部门
      async editMainDeptassort(subjectId) {
        this.projectFormEditDeptassort.supportDeptIds = '';  //改为0防止没发做验证
        this.projectFormEditDeptassort.deptNameEditDeptassort = '';  //改为0防止没发做验证
        await getSubjectInfo(subjectId).then(response => {
          this.deptNameEditDeptassort = response.data.data.supportDeptName;
        });
        this.projectFormEditDeptassort.subjectId = subjectId;
        this.dialogVisibleEditDeptassort = true;
      },
      cancelEditDept() {
        this.dialogVisibleEditDept = false;
      },
      // 弹出协同部门弹框
      clickjointEditDept() {
        this.deptVisible = true
      },
      cancelDept(deptIds, deptNames) {
        this.deptVisible = false;
      },
      getSelectInfo(deptId, deptName) {
        this.projectFormEditDept.supportDeptName = deptName;
        this.projectFormEditDept.deptId = deptId;
        this.deptVisible = false;
      },
      comitYesEditDept() {
        if (this.projectFormEditDept.deptId == 0) {
          this.$notify({
            title: '提示',
            message: '请选择牵头单位',
            type: 'warning',
            duration: 2000
          });
          return;
        }
        this.projectFormEditDept.excludeNull = true;
        subjectEditDept(this.projectFormEditDept).then(response => {
          if (response.data.code == 0) {
            this.$notify({
              title: '成功',
              message: '成功',
              type: 'success',
              duration: 2000
            });
            this.dialogVisibleEditDept = false;
            this.projectFormEditDept.supportDeptName = '';
            this.projectFormEditDept.deptId = 0;
            this.getResultListPage(this.page);
          }
        })
      },
      putTime() {
        var myDate = new Date();
        var month = myDate.getMonth()+1;
        this.subjectForm.time = myDate.getFullYear() + '-' + month + '-' + myDate.getDate();
      },
      cancel() {
        this.$confirm("确定取消吗?", "提示", {
          confirmButtonText: "是",
          cancelButtonText: "否",
          type: "warning"
        }).then(() => {
          this.$router.go(-1);
        })
      },
      confirmYes() {
        for (var item in this.subjectOpinionListData) {
          if (this.subjectOpinionListData[item].deptName == null) {
            this.$notify({
              title: '提示',
              message: '请指定牵头单位',
              type: 'warning',
              duration: 2000
            });
            return;
          } else if (this.subjectOpinionListData[item].description == null) {
            this.$notify({
              title: '提示',
              message: '请填写课题概述',
              type: 'warning',
              duration: 2000
            });
            return;
          }
        }
        this.$refs.subjectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定要编制计划吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.subjectForm.achievementId = this.$route.params.id;
              this.subjectForm.subjectIds = this.$route.query.selectedSubjectIds.split(',');
              addSubjectPlan(this.subjectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: '成功',
                    message: '编制成功',
                    type: 'success',
                    duration: 2000
                  });
                  this.$router.go(-1)
                }
              })
            })
          }
        })
      },
      // 弹出协同部门弹框
      clickjointassortEditDept() {
        this.supportDeptVisible = true
      },
      cancelSupportDept(deptIds, deptNames) {
        this.supportDeptVisible = false;
      },
      getSupportDeptCheckInfo(deptIds, deptNames) {
        this.projectFormEditDeptassort.deptNameEditDeptassort = deptNames;
        this.projectFormEditDeptassort.supportDeptIds = deptIds;
        this.supportDeptVisible = false;
      },
      comitYesEditDeptassort() {
        if (this.projectFormEditDeptassort.supportDeptIds == '') {
          this.$notify({
            title: '提示',
            message: '请选择配合单位',
            type: 'warning',
            duration: 2000
          });
          return;
        }
        this.projectFormEditDeptassort.excludeNull = true;
        subjectEditDept(this.projectFormEditDeptassort).then(response => {
          if (response.data.code == 0) {
            this.$notify({
              title: '成功',
              message: '成功',
              type: 'success',
              duration: 2000
            });
            this.dialogVisibleEditDeptassort = false;
            this.projectFormEditDeptassort = {};
            this.getResultListPage(this.page);
          }
        })
      },

    }
  }
</script>

<style lang="scss" scoped>
  .btn-footer {
    text-align: center;
    margin-bottom: 20px;
  }

  .form-item {
    font-weight: 600;
  }
</style>
